
import collections

# namedtuple 用以构建只有少数属性但是没有方法的对象，比如数据库条目。
Card = collections.namedtuple('Card', ['rank', 'suit'])


class FrenchDeck:
    ranks = [str(n) for n in range(2, 11)] + list('JQKA')
    suits = 'spades diamonds clubs hearts'.split()

    def __init__(self):
        self._cards = [Card(rank, suit) for suit in self.suits
                       for rank in self.ranks]

    def __len__(self):
        return len(self._cards)

    def __getitem__(self, position):
        return self._cards[position]


if __name__ == '__main__':
    from random import choice

    deck = FrenchDeck()
    # print(choice(deck))

    suit_values = dict(spades=3, hearts=2, diamonds=1, clubs=0)

    print("suit_values:", suit_values)


    def spades_high(card):
        rank_value = FrenchDeck.ranks.index(card.rank)
        print("rank_value:", rank_value)
        return rank_value * len(suit_values) + suit_values[card.suit]

    # for card in deck:
    #     print("card:", card)
    #     print(spades_high(card))

    # 也可通过testfile()运行doctest
    # import doctest
    # doctest.testfile("c_error_frenchdeck.doctest")
